from PyQt5.QtWidgets import (
    QDialog, QVBoxLayout, QHBoxLayout, QLabel,
    QFrame, QPushButton, QTextEdit
)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont


class ChanStateDialog(QDialog):
    """缠论状态对话框"""

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("缠论状态分析")
        self.setMinimumSize(400, 300)

        # 主布局
        layout = QVBoxLayout()
        layout.setSpacing(10)
        layout.setContentsMargins(15, 15, 15, 15)

        # 标题
        title_label = QLabel("当前缠论状态")
        title_label.setFont(QFont("Arial", 14, QFont.Bold))
        title_label.setAlignment(Qt.AlignCenter)
        layout.addWidget(title_label)

        # 分隔线
        separator = QFrame()
        separator.setFrameShape(QFrame.HLine)
        separator.setFrameShadow(QFrame.Sunken)
        layout.addWidget(separator)

        # 状态信息布局
        state_layout = QVBoxLayout()
        state_layout.setSpacing(8)

        # 当前分型
        fractal_layout = QHBoxLayout()
        fractal_label = QLabel("当前分型:")
        fractal_label.setFont(QFont("Arial", 10, QFont.Bold))
        self.fractal_value = QLabel("未完成")
        fractal_layout.addWidget(fractal_label)
        fractal_layout.addStretch()
        fractal_layout.addWidget(self.fractal_value)
        state_layout.addLayout(fractal_layout)

        # 当前笔
        pen_layout = QHBoxLayout()
        pen_label = QLabel("当前笔:")
        pen_label.setFont(QFont("Arial", 10, QFont.Bold))
        self.pen_value = QLabel("未完成")
        pen_layout.addWidget(pen_label)
        pen_layout.addStretch()
        pen_layout.addWidget(self.pen_value)
        state_layout.addLayout(pen_layout)

        # 当前中枢
        zs_layout = QHBoxLayout()
        zs_label = QLabel("当前中枢:")
        zs_label.setFont(QFont("Arial", 10, QFont.Bold))
        self.zs_value = QLabel("未形成")
        zs_layout.addWidget(zs_label)
        zs_layout.addStretch()
        zs_layout.addWidget(self.zs_value)
        state_layout.addLayout(zs_layout)

        # 中枢最高价
        high_layout = QHBoxLayout()
        high_label = QLabel("中枢最高价:")
        high_label.setFont(QFont("Arial", 10, QFont.Bold))
        self.high_value = QLabel("--")
        high_layout.addWidget(high_label)
        high_layout.addStretch()
        high_layout.addWidget(self.high_value)
        state_layout.addLayout(high_layout)

        # 中枢最低价
        low_layout = QHBoxLayout()
        low_label = QLabel("中枢最低价:")
        low_label.setFont(QFont("Arial", 10, QFont.Bold))
        self.low_value = QLabel("--")
        low_layout.addWidget(low_label)
        low_layout.addStretch()
        low_layout.addWidget(self.low_value)
        state_layout.addLayout(low_layout)

        layout.addLayout(state_layout)

        # 详细信息区域
        details_label = QLabel("详细分析:")
        details_label.setFont(QFont("Arial", 10, QFont.Bold))
        layout.addWidget(details_label)

        self.details_text = QTextEdit()
        self.details_text.setReadOnly(True)
        self.details_text.setFont(QFont("Consolas", 9))
        layout.addWidget(self.details_text)

        # 按钮
        btn_close = QPushButton("关闭")
        btn_close.clicked.connect(self.accept)
        layout.addWidget(btn_close, alignment=Qt.AlignCenter)

        self.setLayout(layout)

    def update_state(self, state_data):
        """更新缠论状态数据"""
        # 更新基本状态
        self.fractal_value.setText(state_data.get("current_fractal", "未完成"))
        self.pen_value.setText(state_data.get("current_pen", "未完成"))
        self.zs_value.setText(state_data.get("current_zs", "未形成"))

        # 更新价格
        high_price = state_data.get("zs_high", "--")
        low_price = state_data.get("zs_low", "--")

        if isinstance(high_price, float):
            self.high_value.setText(f"{high_price:.4f}")
        else:
            self.high_value.setText(str(high_price))

        if isinstance(low_price, float):
            self.low_value.setText(f"{low_price:.4f}")
        else:
            self.low_value.setText(str(low_price))

        # 更新详细分析
        details = state_data.get("details", "没有详细信息")
        self.details_text.setText(details)